package com.easemob.chat;

import android.util.Base64;
import com.easemob.util.EMLog;
import com.easemob.util.EasyUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;

public class EMEncryptUtils
{
  private static final String TAG = "encrypt";

  static String encryptMessage(String paramString1, String paramString2)
  {
    try
    {
      EncryptProvider localEncryptProvider = EMChatManager.getInstance().getEncryptProvider();
      byte[] arrayOfByte1 = paramString1.getBytes("UTF-8");
      EMLog.d("encrypt", "utf-8 bytes:" + EasyUtils.convertByteArrayToString(arrayOfByte1));
      byte[] arrayOfByte2 = localEncryptProvider.encrypt(arrayOfByte1, paramString2);
      EMLog.d("encrypt", "encrypted bytes:" + EasyUtils.convertByteArrayToString(arrayOfByte2));
      byte[] arrayOfByte3 = Base64.encode(arrayOfByte2, 0);
      EMLog.d("encrypt", "base64 bytes:" + EasyUtils.convertByteArrayToString(arrayOfByte3));
      String str = new String(arrayOfByte3);
      EMLog.d("encrypt", "encrypted str:" + str);
      return str;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      EMLog.e("encrypt", "encryption error, send plain msg");
    }
    return paramString1;
  }

  static String decryptMessage(String paramString1, String paramString2)
  {
    try
    {
      EMLog.d("encrypt", "encrypted str:" + paramString1);
      byte[] arrayOfByte1 = Base64.decode(paramString1, 0);
      EMLog.d("encrypt", "base64 decode bytes:" + EasyUtils.convertByteArrayToString(arrayOfByte1));
      EncryptProvider localEncryptProvider = EMChatManager.getInstance().getEncryptProvider();
      byte[] arrayOfByte2 = localEncryptProvider.decrypt(arrayOfByte1, paramString2);
      EMLog.d("encrypt", "decrypt bytes:" + EasyUtils.convertByteArrayToString(arrayOfByte2));
      String str = new String(arrayOfByte2, "UTF-8");
      EMLog.d("encrypt", "descripted str:" + str);
      return str;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return paramString1;
  }

  public static String encryptFile(String paramString1, String paramString2)
  {
    try
    {
      EMLog.d("encrypt", "try to encrypt file:" + paramString1);
      RandomAccessFile localRandomAccessFile = new RandomAccessFile(paramString1, "r");
      int i = (int)localRandomAccessFile.length();
      EMLog.d("encrypt", "try to encrypt file:" + paramString1 + " original len:" + i);
      byte[] arrayOfByte1 = new byte[i];
      int j = localRandomAccessFile.read(arrayOfByte1);
      if (j != i)
      {
        EMLog.e("encrypt", "error read file, file len:" + i + " readLen:" + j);
        return paramString1;
      }
      localRandomAccessFile.close();
      EncryptProvider localEncryptProvider = EMChatManager.getInstance().getEncryptProvider();
      byte[] arrayOfByte2 = localEncryptProvider.encrypt(arrayOfByte1, paramString2);
      String str1 = null;
      int k = paramString1.lastIndexOf('.');
      if (k >= 0)
        str1 = paramString1.substring(k);
      File localFile = File.createTempFile("encrypted", str1);
      FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
      localFileOutputStream.write(arrayOfByte2);
      localFileOutputStream.close();
      String str2 = localFile.getAbsolutePath();
      EMLog.d("encrypt", "generated encrypted file:" + str2);
      return str2;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return paramString1;
  }

  public static void decryptFile(String paramString1, String paramString2)
  {
    try
    {
      EMLog.d("encrypt", "decrypt file:" + paramString1);
      RandomAccessFile localRandomAccessFile = new RandomAccessFile(paramString1, "r");
      int i = (int)localRandomAccessFile.length();
      byte[] arrayOfByte1 = new byte[i];
      int j = localRandomAccessFile.read(arrayOfByte1);
      if (j != i)
      {
        EMLog.e("encrypt", "error read file, file len:" + i + " readLen:" + j);
        return;
      }
      localRandomAccessFile.close();
      EncryptProvider localEncryptProvider = EMChatManager.getInstance().getEncryptProvider();
      byte[] arrayOfByte2 = localEncryptProvider.decrypt(arrayOfByte1, paramString2);
      FileOutputStream localFileOutputStream = new FileOutputStream(paramString1, false);
      localFileOutputStream.write(arrayOfByte2);
      localFileOutputStream.close();
      EMLog.d("encrypt", "decrypted file:" + paramString1);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.EMEncryptUtils
 * JD-Core Version:    0.6.1
 */